package com.pdwbdn.views;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MsgDialog extends JDialog implements ActionListener {
    private final JLabel label;
    private volatile static MsgDialog instance;
    private MsgDialog() {
        super();
        this.setModal(true);
        this.setSize(300, 200);
        this.setResizable(false);//不允许调整大小
        this.setLocationRelativeTo(null);//居中显示
        this.setUndecorated(true);//去掉最小化关闭那个边框
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.getContentPane().setBackground(Color.PINK);
        label = new JLabel();
        label.setFont(MyFont.font14);
        label.setHorizontalAlignment(JLabel.CENTER);
        this.getContentPane().add(label, BorderLayout.CENTER);
        JButton button = new JButton("OK");
        button.setFont(MyFont.font14);
        button.addActionListener(this);
        this.getContentPane().add(button, BorderLayout.SOUTH);
    }

    public static MsgDialog getInstance() {
        if (instance == null) {
            synchronized (MsgDialog.class) {
                if (instance == null) {
                    instance = new MsgDialog();
                }
            }
        }
        return instance;
    }

    public void showMsg(String msg) {
        label.setText("<html>" + msg + "</html>");
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        this.setVisible(false);
    }
}
